package com.quick.calcu.controller;

import com.quick.calcu.entity.ReviewPlan;
import com.quick.calcu.service.ReviewPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/review-plans")
public class ReviewPlanController {

    @Autowired
    private ReviewPlanService reviewPlanService;

    // 根据ID查询复习计划
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getReviewPlan(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ReviewPlan plan = reviewPlanService.getWithDetails(id);
            if (plan != null) {
                result.put("code", "200");
                result.put("message", "查询成功");
                result.put("data", plan);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "复习计划不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 根据学生ID查询复习计划列表
    @GetMapping("/student/{studentId}")
    public ResponseEntity<Map<String, Object>> getByStudentId(@PathVariable Long studentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ReviewPlan> plans = reviewPlanService.getPlansByStudentId(studentId);
            result.put("success", true);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", plans);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 添加复习计划
    @PostMapping
    public ResponseEntity<Map<String, Object>> addReviewPlan(@RequestBody ReviewPlan plan) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = reviewPlanService.save(plan);
            result.put("success", success);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 标记复习完成
    @PostMapping("/{planId}/complete")
    public ResponseEntity<Map<String, Object>> completeReview(@PathVariable Long planId) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = reviewPlanService.updateReviewCount(planId, 1);
            result.put("success", success);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "复习完成" : "操作失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "操作失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}